import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import ViewUI from 'view-design';
import i18n from '@/locale'
import config from '@/config'
import axios from 'axios'
import VueAxios from 'vue-axios'
import importDirective from '@/directive'
import '@/styles/iview.less';
import Clickoutside from 'element-ui/src/utils/clickoutside'
// import VueAuth from '@websanova/vue-auth'
import 'element-ui/lib/theme-chalk/index.css'
import installPlugin from '@/plugin'
import './index.less'
import Element from 'element-ui'
import md5 from 'js-md5'
import table from '@/libs/table'
import '../src/plugin/vue.minix'
import './assets/icon/iconfont.css'
import dataInfo from './libs/data'
import * as echarts from 'echarts';
import utils from '@/libs/util';
import Cookies from 'js-cookie';
import { registerMicroApps, start } from 'qiankun';
import api from '@/api/login';

Vue.prototype.$echarts = echarts;

Vue.prototype.$md5 = md5;

Vue.prototype.$dataInfo = dataInfo;
Vue.prototype.$utils = utils;

Vue.use(ViewUI, {
  i18n: (key, value) => i18n.t(key, value)
})
Vue.use(Element, {
  i18n: (key, value) => i18n.t(key, value)
});


String.prototype.trim = function() {
  return this.replace(/(^\s*)|(\s*$)/g, '');
};

String.prototype.replaceAll = function (FindText, RepText) {
  let regExp = new RegExp(FindText, 'g');
  return this.replace(regExp, RepText);
};

Vue.use(VueAxios, axios);
Vue.router = router;

const baseURL = process.env.NODE_ENV === 'development'
    ? config.baseUrl.dev
    : process.env.VUE_APP_MODE === 'testEnvironment'
        ? config.baseUrl.dev
        :  process.env.VUE_APP_MODE === 'temporary'
            ? config.baseUrl.temporary
            :  process.env.VUE_APP_MODE === 'pre'
                ? config.baseUrl.pre
                :  process.env.VUE_APP_MODE === 'prod'
                    ? config.baseUrl.pro
                    : config.baseUrl.dev;
Vue.axios.defaults.baseURL = baseURL;
Vue.axios.defaults.headers.post['Content-Type'] = 'application/json';
Vue.axios.defaults.headers.get['Content-Type'] = 'application/json';

const wmsUrl = process.env.NODE_ENV === 'development'
    ? config.wmsUrl.dev
    : process.env.VUE_APP_MODE === 'testEnvironment'
        ? config.wmsUrl.dev
        :  process.env.VUE_APP_MODE === 'temporary'
            ? config.wmsUrl.temporary
            :  process.env.VUE_APP_MODE === 'prod'
                ? config.wmsUrl.pro
                : config.wmsUrl.dev;

// 添加请求拦截器
Vue.axios.interceptors.request.use(function (axiosConfig) {
  if (Cookies.get('token') && (!axiosConfig.parameter || axiosConfig.parameter.type !== 'WMS')) {
    axiosConfig.headers.Authorization = Cookies.get('token');
  }
  return axiosConfig;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// Vue.use(VueAuth, {
//   auth: require('@websanova/vue-auth/drivers/auth/bearer.js'),
//   http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
//   router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
//   rolesVar: 'authorities',
//   tokenDefaultName: 'token',
//   authRedirect: {path: '/login'},
//   forbiddenRedirect: {path: '/403'},
//   notFoundRedirect: {path: '/404'},
//   token: [{request: 'Authorization', response: 'Authorization', authType: 'Bearer', foundIn: 'header'}],
//   loginData: {url: '/api/authorizeNew', method: 'POST', redirect: '/login', fetchUser: true},
//   fetchData: {url: '/api/sUser/currentUserInfo', method: 'GET', enabled: true},
//   refreshData: {url: 'auth/refresh', method: 'GET', enabled: false, interval: 30},
//   logoutData: {redirect: '/login', makeRequest: false},
//   parseUserData: function (data) {
//     if (data && data.returnData && data.returnData.fieldSetting) {
//       localStorage.fieldRequireSetting = JSON.stringify(data.returnData.fieldSetting);
//     }
//     return data.returnData;
//   }
// });
/**
 * @description 注册admin内置插件
 */
installPlugin(Vue)
/**
 * @description 生产环境关掉提示
 */
Vue.config.productionTip = false
/**
 * @description 全局注册应用配置
 */
Vue.prototype.$config = config;
Vue.prototype.$table = table;
/**
 * 注册指令
 */
importDirective(Vue)
Vue.directive('clickoutside', Clickoutside)
const getActiveRule = hash => location => {
  console.log(location.hash);
  return  location.hash.startsWith(hash);
};
// const ccisUrl = process.env.NODE_ENV === 'development'
//     ? config.ccisUrl.dev
//     : process.env.VUE_APP_MODE === 'testEnvironment'
//         ? config.ccisUrl.dev
//         :  process.env.VUE_APP_MODE === 'temporary'
//             ? config.ccisUrl.temporary
//             :  process.env.VUE_APP_MODE === 'pre'
//                 ? config.ccisUrl.pre
//                 :  process.env.VUE_APP_MODE === 'prod'
//                     ? config.ccisUrl.pro
//                     : config.ccisUrl.dev;
// registerMicroApps([
//   {
//     name: 'platformApp', // app name registered
//     // entry: 'http://192.168.102.138:8192/CCIS',
//     // entry: 'http://localhost:8080/CCIS',
//     entry: ccisUrl,
//     container: '#platformApp',
//     activeRule: getActiveRule('#/platformApp'),
//     props: {
//       router
//     }
//   }
// ]);

new Vue({
  el: '#app',
  router,
  i18n,
  store,
  render: h => h(App),
  async created () {
    const lang = localStorage.getItem('language');
    this.$i18n.locale = lang || 'zh-CN';
  }
})
if (!window.qiankunStarted) {
  window.qiankunStarted = true;
  start({prefetch: 'all'});
}

console.log('Environment:', config.env);
window.loggerD = (...msg) => {
  if (config.env !== 'development') {
    return;
  }
  console.log(...msg);
};
window.loggerE = (...err) => {
  if (config.env !== 'development') {
    return;
  }
  console.error(...err);
};
window.onerror = function (message, url, line, column, error) {
  console.log('log---onerror::::', message, url, line, column, error);
};

// window.addEventListener('popstate', function() {
//   history.pushState(null, null, document.URL)
// })
